package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author 32338
 * @date 2024-07-01 16:27:51
 * @description 消费者接收消息
 * 第一步：引入SpringAMQP的依赖
 * 第二步：配置RabbitMQ的地址
 */

@Component
public class SpringRabbitListener {
/*
    @RabbitListener(queues = "simple.queue")//第三步，指定队列的名称
    public void listenSimpleQueue(String message){

        System.out.println("消费者接收到simple.queue的消息："+message);

    }*/


    @RabbitListener(queues = "simple.queue")//第三步，指定队列的名称
    public void listenSimpleQueue1(String message) throws InterruptedException {

        System.out.println("消费者1接收到simple.queue的消息："+message+LocalDateTime.now());
        Thread.sleep(20);//消息预取机制，拿了奇数的消息，MQ消息队列默认使用轮询的机制

    }
    @RabbitListener(queues = "simple.queue")//第三步，指定队列的名称
    public void listenSimpleQueue2(String message) throws InterruptedException {

        System.err.println("消费者2接收到simple.queue的消息："+message+ LocalDateTime.now());
        Thread.sleep(200);//如果需要让他处理更少的消息，我们需要设置preFetch的值，来控制消息预取的上线

    }

    @RabbitListener(queues = "fanout.queue1")//第三步，指定队列的名称
    public void listenFanoutQueue1(String message){

        System.out.println("消费者1接收到fanout.queue的消息：【"+message+"】");

    }
    @RabbitListener(queues = "fanout.queue2")//第三步，指定队列的名称
    public void listenFanoutQueue2(String message){

        System.out.println("消费者2接收到fanout.queue的消息：【"+message+"】");

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct" ,type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者接收到direct.queue1的消息:【"+msg+"】 ");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct" ,type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("消费者接收到direct.queue2的消息:【"+msg+"】 ");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic" ,type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者接收到topic.queue1的消息:【"+msg+"】 ");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic" ,type = ExchangeTypes.TOPIC),//切换为话题发布类型Topic
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者接收到topic.queue2的消息:【"+msg+"】 ");
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String ,Object> msg){
        System.out.println("消费者接收到object.queue的消息:【"+msg+"】 ");
    }
}
